﻿// CTileCursor.cs
// Author: Gant Briseno
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Casters.source.Managers;
#endregion

namespace Casters.source.Objects
{
    public class CTileCursor : CRenderObject
    {
        Vector2 m_vPositionOnMap;
        Vector2 m_vCenterPointOffset;
        float m_fHeightOffset;
        Color m_OldTileColor;
        Color m_NewTileColor;

        public CTileCursor(Vector2 _mapPos) : base() 
        {
            m_vPositionOnMap = _mapPos;
            m_fHeightOffset = 150f;
            m_OldTileColor = Color.White;
            m_NewTileColor = Color.LimeGreen;
        }

        public override void LoadContent()
        {
            SetupRenderObject(0, CGame.GetInstance().Content.Load<Texture2D>("tilecursor") as Texture2D, Vector2.Zero, new Vector2(1f, 1f), new Vector2(64f, 64f), "none");
            m_vCenterPointOffset = new Vector2(32f, 32f);
            m_OldTileColor = CTileManager.GetInstance().ChangeTileColor(m_vPositionOnMap, m_NewTileColor);

            CEventSystem.RegisterEventHandler(EventID.Move_TileCursorUp, OnMoveUp);
            CEventSystem.RegisterEventHandler(EventID.Move_TileCursorDown, OnMoveDown);
            CEventSystem.RegisterEventHandler(EventID.Move_TileCursorLeft, OnMoveLeft);
            CEventSystem.RegisterEventHandler(EventID.Move_TileCursorRight, OnMoveRight);
            CEventSystem.RegisterEventHandler(EventID.TileCursor_Kill, OnKill);
            base.LoadContent();
        }

        public override void UnloadContent()
        {
            CEventSystem.UnregisterEventHandler(EventID.Move_TileCursorUp, OnMoveUp);
            CEventSystem.UnregisterEventHandler(EventID.Move_TileCursorDown, OnMoveDown);
            CEventSystem.UnregisterEventHandler(EventID.Move_TileCursorLeft, OnMoveLeft);
            CEventSystem.UnregisterEventHandler(EventID.Move_TileCursorRight, OnMoveRight);
            CEventSystem.UnregisterEventHandler(EventID.TileCursor_Kill, OnKill);
            base.UnloadContent();
        }

        public override void Update(float dt)
        {
            base.Update(dt);
        }

        public override void Draw()
        {
            Vector2 RenderPos = CTileManager.GetInstance().GetTileCenterpointPosition(m_vPositionOnMap) - m_vCenterPointOffset;
            m_vPosition = RenderPos;
            m_vPosition.Y -= m_fHeightOffset;
            base.Draw();
        }

        public void OnMoveUp(params object[] eventArgs)
        {
            if (m_vPositionOnMap.Y - 1 >= 0)
            {
                CTileManager.GetInstance().ChangeTileColor(m_vPositionOnMap, m_OldTileColor);
                m_vPositionOnMap.Y -= 1;
                m_OldTileColor = CTileManager.GetInstance().ChangeTileColor(m_vPositionOnMap, m_NewTileColor);
            }
        }

        public void OnMoveDown(params object[] eventArgs)
        {
            if (m_vPositionOnMap.Y + 1 < CTileManager.GetInstance().m_vMapSize.Y)
            {
                CTileManager.GetInstance().ChangeTileColor(m_vPositionOnMap, m_OldTileColor);
                m_vPositionOnMap.Y += 1;
                m_OldTileColor = CTileManager.GetInstance().ChangeTileColor(m_vPositionOnMap, m_NewTileColor);
            }
        }

        public void OnMoveLeft(params object[] eventArgs)
        {
            if (m_vPositionOnMap.X - 1 >= 0)
            {
                CTileManager.GetInstance().ChangeTileColor(m_vPositionOnMap, m_OldTileColor);
                m_vPositionOnMap.X -= 1;
                m_OldTileColor = CTileManager.GetInstance().ChangeTileColor(m_vPositionOnMap, m_NewTileColor);
            }
        }

        public void OnMoveRight(params object[] eventArgs)
        {
            if (m_vPositionOnMap.X + 1 < CTileManager.GetInstance().m_vMapSize.X)
            {
                CTileManager.GetInstance().ChangeTileColor(m_vPositionOnMap, m_OldTileColor);
                m_vPositionOnMap.X += 1;
                m_OldTileColor = CTileManager.GetInstance().ChangeTileColor(m_vPositionOnMap, m_NewTileColor);
            }
        }

        public void OnKill(params object[] eventArgs)
        {
            m_bIsAlive = false;
        }
    }
}
